
import Head from 'next/head'

<Head>
  <script>
    {
      `(function() {
         var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?e60fb290e204e04c5cb6f79b0ac1e697";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
       })();`
    }
  </script>
</Head>

![LangChain](https://pica.zhimg.com/50/v2-56e8bbb52aa271012541c1fe1ceb11a2_r.gif)





TF-IDF检索器[＃](#tf-idf-retriever "永久链接到此标题")
==========================================

本教程概述了如何使用使用scikit-learn的TF-IDF的检索器。

有关TF-IDF详细信息，请参见[此博客文章](https://medium.com/data-science-bootcamp/tf-idf-basics-of-information-retrieval-48de122b2a4c)。

```python
from langchain.retrievers import TFIDFRetriever

```

```python
# !pip install scikit-learn

```

使用文本创建新的检索器[＃](#create-new-retriever-with-texts "永久链接到此标题")
-----------------------------------------------------------

```python
retriever = TFIDFRetriever.from_texts(["foo", "bar", "world", "hello", "foo bar"])

```

使用检索器[＃](#use-retriever "永久链接到此标题")
-----------------------------------

现在我们可以使用检索器了！

```python
result = retriever.get_relevant_documents("foo")

```

```python
result

```

```python
[Document(page_content='foo', metadata={}),
 Document(page_content='foo bar', metadata={}),
 Document(page_content='hello', metadata={}),
 Document(page_content='world', metadata={})]

```

